package otherapi.jackson;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.lidong.toolkit.utils.JackSonUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 3/8/2023 8:50 AM
 */
public class JacksonMultiInstanceTest {

    private static final Logger logger = LoggerFactory.getLogger(JacksonMultiInstanceTest.class);

    @Test
    public void test1() {
        String tempStr = "{\"speed\":\"100\",\"name\":\"cat\",\"food\":\"dogfood\"}";
        ObjectMapper mapper = new ObjectMapper();
        // 配置忽略未知属性
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            Dog dog = (Dog) mapper.readValue(tempStr, Animal.class);
            logger.info("cat:{} ", dog.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test2() {
        String tempStr = "{\"speed\":\"100\",\"name\":\"dog\",\"food\":\"dogfood\",\"id\":\"1001\"}";
        ObjectMapper mapper = new ObjectMapper();
        // 配置忽略未知属性
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            Dog dog = (Dog) mapper.readValue(tempStr, Dog.class);
            logger.info("cat:{} ", dog.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test3() {
        String tempStr = "{\"speed\":\"100\",\"name\":\"dog\",\"food\":\"dogfood\"}";
        Dog dog = JSONObject.parseObject(tempStr, Dog.class);
        Dog dog1 = JackSonUtils.parseObject(tempStr, Dog.class);
        logger.info("dog:{} ", dog);
    }

    @Test
    public void test4() {
        ObjectMapper objectMapper = new ObjectMapper();
        // 属性为Null的不进行序列化，只对pojo起作用，对map和list不起作用
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // json进行换行缩进等操作
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        // json不进行换行缩进等操作  默认就是不进行操作，写了这行和没写的效果一样
        objectMapper.disable(SerializationFeature.INDENT_OUTPUT);
        // json是否允许属性名没有引号 ，默认是false
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        //json是否允许属性名为单引号 ，默认是false
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        // 遇到未知属性是否抛出异常 ，默认是抛出异常的
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 当实体类没有setter方法时，序列化不报错，返回一个空对象
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        // 所有的字母小写，没有分隔符，例如 lowercase
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE);
        //序列化的时候序列对象的所有属性
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        // 设置日期格式化
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

}
